﻿using System;
using System.Collections.Generic;
using System.Web;
using ihongma.Common;
using System.Web.SessionState;

using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;
using System.Collections;
namespace ihongma.Web.tools
{
    /// <summary>
    /// imagestool 的摘要说明
    /// </summary>
    public class imagestool : IHttpHandler, IRequiresSessionState
    {

        EncodingOptions options = null;
        BarcodeWriter writer = null;
        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = DTRequest.GetQueryString("action");

            switch (action)
            {
                case "Barcode": //验证扩展字段是否重复
                    Barcode(context);
                    break;
                case "Qrcode": //验证扩展字段是否重复
                    Qrcode(context);
                    break;
                default: //普通上传
                    err(context);
                    break;
            }
        }

        public void Barcode(HttpContext context)
        {
            
            string code = DTRequest.GetQueryString("code");
            
            //MultiFormatWriter mutiWriter = new MultiFormatWriter();
            //BitMatrix bm = mutiWriter.encode(code, BarcodeFormat.EAN_13, 220, 110);
                //清除该页输出缓存，设置该页无缓存 
            options = new  QrCodeEncodingOptions
            {
                //DisableECI = true,
                CharacterSet = "UTF-8",
                Width = 220,
                Height = 110,
             Margin=0
            };
            QRCodeWriter qrwirter = new QRCodeWriter();
            var hints = new Dictionary<EncodeHintType, object>();
           
            hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
               
                //context.Response.AppendHeader("Pragma", "No-Cache");
            Bitmap bmp = qrwirter.encode(code, BarcodeFormat.QR_CODE, 200, 200, hints).ToBitmap();
                MemoryStream ms = new MemoryStream();
                try
                {
                    bmp.Save(ms, ImageFormat.Gif);
                    context.Response.ClearContent();
                    context.Response.ContentType = "image/gif";
                    context.Response.BinaryWrite(ms.ToArray());
                }
                finally
                {
                    //显式释放资源 
                    bmp.Dispose();

                }
            }
        public void Qrcode(HttpContext context)
        {

            string code = DTRequest.GetQueryString("code");
           
            options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8",
                Width = 200,
                Height = 200,
             Margin=0
            };
            
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
              // writer.Encoder=
                //context.Response.AppendHeader("Pragma", "No-Cache");
            Bitmap bmp = writer.Write(code);
            MemoryStream ms = new MemoryStream();
            try
            {
                bmp.Save(ms, ImageFormat.Gif);
                context.Response.ClearContent();
                context.Response.ContentType = "image/jpg";
                context.Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                //显式释放资源 
                bmp.Dispose();

            }
        }
        public void err(HttpContext context)
        {
            context.Response.Write("0");
            context.Response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}